﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Cms.Web.CmsSystem.SysAdmin.FormSetting
{
    /// <summary>
    /// FormHandler 的摘要说明
    /// </summary>
    public class FormHandler : IHttpHandler
    {
        private static BLL.Sys_Selfform _bllForm = null;
        private static BLL.Sys_Selfform bllForm
        {
            get
            {
                if (_bllForm == null)
                {
                    _bllForm = new BLL.Sys_Selfform();
                }
                return _bllForm;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            string FormId = EntLib.Request.GetString("formid");
            string Action = EntLib.Request.GetString("action");
            string Id = EntLib.Request.GetString("id");
            string Result = "";
            switch (Action.ToLower())
            {
                case "update":
                    Result = Sumbit(FormId, "");
                    break;
                case "showinfo":
                    Result = ShowInfo(FormId, Id);
                    break;
                default:
                    break;
            }
            EntLib.Response.ResponseText(Result);
        }
        public string Sumbit(string formId, string RecordId)
        {
            Model.Sys_Selfform model = bllForm.GetById(formId, false);
            if (model != null)
            {
                if (model.Datatype.Equals("obj", StringComparison.CurrentCultureIgnoreCase))
                {
                    string ClassName = model.ClassName;
                    Type type = typeof(Model.Sys_Selfform).Assembly.GetType(ClassName);
                    if (type != null)
                    {
                        System.Collections.Specialized.NameValueCollection DictMain = HttpContext.Current.Request.Form;//(Dictionary<string, object>)obj;                        
                        object objModel = EntLib.Utiltiy.DictToModel(DictMain, type);
                        Type blltype = Core.GetBLLTypeByFormID("", type);
                        object bll = Activator.CreateInstance(blltype);
                        blltype.GetMethod("SaveOrUpdate").Invoke(bll, new object[] { objModel });
                        blltype.GetMethod("CommitChanges").Invoke(bll, null);
                    }
                    else
                    {
                        //类名不正确
                    }
                }
                //object objModel = EntLib.Utiltiy.DictToModel(DictMain, type);

            }
            string result = "";
            return result;
        }
        public string ShowInfo(string FormId, string RecordId)
        {
            string result = "";
            Model.Sys_Selfform model = bllForm.GetById(FormId, false);
            if (model != null && !String.IsNullOrEmpty(RecordId))
            {
                string ClassName = model.ClassName;
                Type type = typeof(Model.Sys_Selfform).Assembly.GetType(ClassName);
                Type blltype = Core.GetBLLTypeByFormID("", type);
                object bll = Activator.CreateInstance(blltype);
                object objModel = blltype.GetMethod("GetById").Invoke(bll, new object[] { RecordId, false });
                if (objModel != null)
                {
                    result = EntLib.Json.ToJson(objModel);
                }
            }
            return result;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}